package cn.xawl.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.xawl.gulimall.product.entity.AttrAttrgroupRelationEntity;
import cn.xawl.gulimall.product.entity.BrandEntity;
import cn.xawl.gulimall.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.xawl.gulimall.product.entity.CategoryBrandRelationEntity;
import cn.xawl.gulimall.product.service.CategoryBrandRelationService;
import cn.xawl.common.utils.PageUtils;
import cn.xawl.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author lh
 * @email lhm49869999@163.com
 * @date 2020-06-17 14:15:22
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 获取品牌关联的分类
     */
    @GetMapping("/catalog/list")
    public R categoryList(@RequestParam(value = "brandId", required = true) Long brandId){
        List<CategoryBrandRelationEntity>  data = categoryBrandRelationService.getCategoryListById(brandId);
        return R.ok().put("data", data);
    }

    /**
     * 获取分类关联的品牌
     * /product/categorybrandrelation/brands/list
     */
    @GetMapping("/brands/list")
    public R brandList(@RequestParam Long catId){
        List<BrandEntity>  data = categoryBrandRelationService.getBrandListById(catId);

        List<BrandVo> collect = data.stream().map(brandEntity -> {
            BrandVo brandVo = new BrandVo();
            brandVo.setBrandId(brandEntity.getBrandId());
            brandVo.setBrandName(brandEntity.getName());
            return brandVo;
        }).collect(Collectors.toList());

        return R.ok().put("data", collect);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 新增品牌与分类关联关系
     */
    @RequestMapping("/save")
        public R saveDetail(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.saveDetail(categoryBrandRelation);

        return R.ok();
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
        public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
        public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
